#ifndef __UGUI_KEY_H__
#define __UGUI_KEY_H__

#include "cm32m101a.h"
#include "gpio.h"


#define SHORT_TIME          3   
#define LONG_TIME           3000 
#define NONE_ENTER          0
#define LONG_ENTER          1
#define SHORT_ENTER         2

//按键值
#define Key_Set             (0x0004)   //设置按键
#define Key_Return          (0x0040)   //放回按键
#define Key_ADD             (0x0002)   //加号按键 （+）
#define Key_SUD             (0x0020)   //减号按键 （-）
#define Key_BLOOP           (0x0001)   //消音按键
#define Key_TEST            (0x0010)   //测试按键


/*********按键数据*****************/
typedef union _TM1638_KEY_32_T
{
    uint32_t  HEX_32;
    uint16_t  HEX_16[2];
    uint8_t   HEX_8[4];
}KEY_TypeDef;

/*********led指示灯数据*****************/
typedef union _TM1638_LED_32_T
{
    uint16_t   HEX_16;
    struct
    {
        uint8_t open_phase:1;       // 缺相报警  
        uint8_t relays:1;           // 继电器状态灯
        uint8_t leakCurt:1;         // 漏电报警
        uint8_t alarm:1;            // 报警灯
        
        uint8_t overCurt:1;         // 过流报警
        uint8_t message:1;          // 通讯灯
        uint8_t lowVolt:1;          // 欠压报警
        uint8_t run:1;              // 运行灯
        
        uint8_t overVol:1;          // 过压报警
        uint8_t power:1;            // 电源灯
        uint8_t cutCurt :1;         // 短路报警
        uint8_t res: 5;             // 备用
    }bit;
    
}LED_TypeDef;

typedef struct _keydis
{
    uint8_t   PressFlg;               //按下标志
    uint8_t   ProEvent;               //发生按压事件
    uint8_t   FirPre;                 //按键松开标志
    uint8_t   PressStatus;            //按压标志 ：按下/弹起
    uint8_t   KeyStatus;              //按键状态 ：长按/短按
    
    uint8_t   DnCnt;                  //按下次数
    uint8_t   UpCnt;                  //抬起次数
    uint8_t   QueryDispCnt;           //查询显示计数
    uint8_t   CursorCnt;              //游标
    
    uint16_t  cnt;                    //按键按下计时计数 ++
    uint32_t  KeyVal;                 //按键芯片数值
}keydis_t;

extern keydis_t keydis;
extern KEY_TypeDef KeyValue;              //保存按键值
extern LED_TypeDef LedValue;              //保存led灯

uint16_t Feisuo_process(uint16_t min, uint16_t max, uint16_t num);
uint16_t Feisuo_process_Return(uint16_t min, uint16_t max, uint16_t num);

void key_led_handle(void);

void Key_ADD_SUD_Parameter(uint8_t key_id);
void Key_Set_Parameter(void);

#endif   //__UGUI_KEY_H__

